package com.vids;

import com.vids.controller.Test;
import com.vids.hikcam.HIKCamAlarm;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.List;

/**
 * @author ly
 */
@Slf4j
@EnableScheduling
@EnableTransactionManagement
@SpringBootApplication
@MapperScan(basePackages = {"com.vids.mapper"})
public class VIDSApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(VIDSApplication.class, args);
        HIKCamAlarm.setApplicationContext(run);
        Test bean = run.getBean(Test.class);
        bean.getSDK();

    }
    @Bean
    public RestTemplate restTemplate() {
        // 设置restTemplate的超时时间
        OkHttp3ClientHttpRequestFactory httpRequestFactory = new OkHttp3ClientHttpRequestFactory();
        httpRequestFactory.setConnectTimeout(1000);
        httpRequestFactory.setReadTimeout(3000);
        RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
        // 解决远程调用乱码的问题
        List<HttpMessageConverter<?>> httpMessageConverters = restTemplate.getMessageConverters();
        httpMessageConverters.stream().forEach(httpMessageConverter -> {
            if (httpMessageConverter instanceof StringHttpMessageConverter) {
                StringHttpMessageConverter messageConverter = (StringHttpMessageConverter) httpMessageConverter;
                messageConverter.setDefaultCharset(Charset.forName("UTF-8"));
            }
        });
        return restTemplate;
    }
}
